<%@ page contentType="application/json; charset=UTF-8"  pageEncoding="UTF-8" errorPage="/error.jsp"%>
<%@page import="dbexp.model.db.DbUser"%>
<%@page import="dbexp.model.DbObject"%>
<%@page import="dbexp.connection.DbMetaData"%>
<%@page import="org.json.JSONObject"%>
<%@page import="java.util.List" %>
<%!
String schema = "";
String status = "";
String connectionName = "";
String created = null;
String expirty = null;
String dbName = "";
String defTablespace = "";
String hostName = "";
String lock = null;
List<DbObject> obj_privs = null;
int port = 1521;
List<String> role = null;
List<String> sys_privs = null;
String tmpTablespace = "";
boolean dba = false;
%>
<%!public boolean isNotEmpty(String param){
	return param!=null && param.trim().length()>0;
}
%>
<%
DbUser u = null;
String action = request.getParameter("action");
if(action!=null){
	String sid = request.getParameter("sid");
	String host = request.getParameter("host");
	String password = request.getParameter("password");
	String port = request.getParameter("port");
	String name = request.getParameter("name");
	if(action.equals("login") || action.equals("check")){
		if(action.equals("login") && isNotEmpty(sid) && isNotEmpty(host) && isNotEmpty(password) && isNotEmpty(port) && isNotEmpty(name)){
	      	u = new DbUser(request.getParameter("sid"),request.getParameter("host"),request.getParameter("password"),Integer.parseInt(request.getParameter("port")),request.getParameter("name"));
	      	u = DbMetaData.createConnection(u);
		}else if(action.equals("check")){
			if(session.getAttribute("USER")!=null){
				u=(DbUser)session.getAttribute("USER");
			}
		}
	      	if(u!=null){
	      		session.setAttribute("USER",u);

	      		schema = u.getSchemaName();
     			status = u.getStatus();
     			connectionName = u.getConnectionName();
     			created = u.getCreated();
     			dbName = u.getDbName();
     			defTablespace = u.getDefaultTablespace();
     			expirty = u.getExpiryDate();
     			hostName = u.getHostName();
     			lock = u.getLockDate();
     			obj_privs = u.getObjectPrivs();
     			port = ""+u.getPortNumber();
     			role = u.getRole();
     			sys_privs = u.getSystemPrivs();
     			tmpTablespace = u.getTemporaryTablespace();
     			dba = u.isAccessToDBA();
	      		out.clearBuffer();%>
	      	<%=new JSONObject().
													put("success", true).
													put("data",new JSONObject().
															put("schema", schema).
															put("status", status).
															put("connection_name", connectionName).
															put("created", created).
															put("sid", dbName).
															put("tablespace", defTablespace).
															put("expiry", expirty).
															put("hostname", hostName).
															put("lock", lock).
															put("object_privs", obj_privs).
															put("port", port).
															put("role", role).
															put("system_privs", sys_privs).
															put("tmp_tablespace", tmpTablespace).
															put("dba", dba))%>
	      	<%
	      	}else{
		      	out.clearBuffer();%>
		      	<%=new JSONObject().
														put("success", false)%>
		      	<%
			}
	}else if(action.equals("logout")){
		if(session.getAttribute("USER")!=null){
			if(DbMetaData.destroyConnection((DbUser)session.getAttribute("USER"))){
		      	out.clearBuffer();%>
		      	<%=new JSONObject().
														put("success", true)%>
		      	<%
			}else{
		      	out.clearBuffer();%>
		      	<%=new JSONObject().
														put("success", false).
														put("error", "Połączenie nie mogło zostać zakończone.!!!")%>
		      	<%
			}
		}else{
	      	out.clearBuffer();%>
	      	<%=new JSONObject().
													put("success", false)%>
	      	<%
		}
	}else{
		      	out.clearBuffer();%>
		      	<%=new JSONObject().
														put("success", false)%>
		      	<%
			}%>
<%}%>